import React from 'react';
import { connect } from 'dva';
import { createForm } from 'rc-form';
import styles from './index.less';
import {DatePicker,Toast,List,InputItem,Button,Modal,WingBlank,WhiteSpace} from 'antd-mobile';
import enUs from 'antd-mobile/lib/date-picker/locale/en_US';
import {routerRedux} from 'dva/router';
import Block from 'fs-flex'
import Header from '../../../../components/Header/index'
import SmsService from '../../../../services/smsService'
const alert = Modal.alert

function formatDate(date) {
      /* eslint no-confusing-arrow: 0 */
      const pad = n => n < 10 ? `0${n}` : n;
      const dateStr = `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())}`;
      const timeStr = `${pad(date.getHours())}:${pad(date.getMinutes())}`;
      return `${timeStr}`;

}

const CustomChildren = ({ extra, onClick, children  }) => (
    <div
        onClick={onClick}
        style={{ backgroundColor: '#fff', height: '45px', lineHeight: '45px', padding: '0 15px'  }}
    >
        {children}
        <span style={{ float: 'right', color: '#888'  }}>{extra}</span>
    </div>

);
class SettingSmsPage extends React.Component{
  state={
      show:false,
      data:[],
      customChildValue:null
  }
    async componentDidMount(){
        const { data,msg   }= await SmsService.getSmsCapacity()
        this.setState({data:data})
        let today = new Date()
        today.setHours(Number(data[1].sysValue.split(":")[0]))
        today.setMinutes(Number(data[1].sysValue.split(":")[1]))
        this.setState({customChildValue:today})
    }
  
    async save(){
        //更新
        let data = this.props.form.getFieldsValue()
        const {code,msg}=await SmsService.updateSmsCapacity(data.capacity,formatDate(this.state.customChildValue),data.arriveTime)
        if(code==="0000"){
            Toast.info("更新成功！",2)
        }else{
            Toast.info(msg,3)
        }
    }
  render(){
      const { getFieldProps  } = this.props.form;
    return (
      <Block vf>
        <Block>
          <Header needBack='true' title='短信参数设置' history={this.props.history}/>
        </Block>
        <WingBlank size="lg">
            <WhiteSpace size="lg" />
                <List className={styles.inputItem}>
                    <InputItem
                            {...getFieldProps('capacity',{initialValue:this.state.data && this.state.data.length>0?this.state.data[0].sysValue:""})}
                            clear
                            labelNumber={7}
                            placeholder="短信业务周期设计（条）"
                            ref={el => this.autoFocusInst = el}
                        >短信业务周期设计（条）：</InputItem>
                    <DatePicker
                        {...getFieldProps('endTime',{initialValue:this.state.data && this.state.data.length>0?this.state.data[1].sysValue:""})}
                        mode="time"
                        format="HH:mm"
                        title="每日通知发送截止时间"
                        value={this.state.customChildValue}
                        onChange={v => this.setState({ customChildValue: v  })}
                        extra=" "
                    >
                        <List.Item>每日通知发送截止时间:</List.Item>
                    </DatePicker>
                    <InputItem
                        {...getFieldProps('arriveTime',{initialValue:this.state.data && this.state.data.length>0?this.state.data[2].sysValue:""})}
                        clear
                        labelNumber={7}
                        placeholder="通知用户后延时间"
                        ref={el => this.autoFocusInst = el}
                    >通知用户后延时间：</InputItem>
                </List>
            <WhiteSpace size="lg" />
            <Block wf mt={15}>
                <Block f='3'></Block>
                <Block f='4'><Button a='c' f='c' size='small' type="primary" onClick={() =>
                        this.save()}
                    >保存</Button></Block>
                <Block f='3'></Block>
            </Block>
        </WingBlank>
      </Block>
    );
  }
  
}

SettingSmsPage.propTypes = {};

export default createForm()(connect()(SettingSmsPage));
